// packageJ/consensus_activity/mass_intergral/mass_intergral.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    text:"",
    time_array:[],
    quota_array:[],
    total_array:[],
    show: false,
    total_page: 1,
    page: 1,

    current_index_time: 0,
    current_index_quota: 0,
    id:"",
    rate:"",
    usable_quota:"",
    total:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getData();
    this.record();
  },
  record(page = 1) {
    app._getNetWork({
      url:app.getNetAddresss('plugin.wise-yuan-trade.frontend.quality-activity.record'),
      data:{page},
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.data.page = data.current_page;
          this.data.total_page = data.last_page;
          if(page==1){
            this.setData({list:data.data});
          }else{
            let list = this.data.list.concat(data.data);
            this.setData({list});
          }
        }else{
          app.tips(msg);
        }
      }
    })
  },
  getData() {
    app._getNetWork({
      url:app.getNetAddresss('plugin.wise-yuan-trade.frontend.quality-activity.index'),
      success:resdata=>{
        let {result,msg,data} = resdata.data;
        if(result){
          this.setData({
            time_array:data.time_array ,
            quota_array:data.quota_array,
            total_array:data.total_array,
            total:data.total_array[0],
            text:data.quality_name
          })
          wx.setNavigationBarTitle({
            title: `${data.quality_name}活动`,
          })
        }else{
          app.tips(msg);
        }
      }
    })

  },
  hidden(){
    this.setData({show:false})
  },
  showPopup(){
    this.setData({show:true})
    this.get_activity();
  },
  toggle(e) {
    let name = e.currentTarget.dataset.name,
    i=e.currentTarget.dataset.i;
    if (name == 'time') {
      this.setData({current_index_time:i})
    } else if (name === 'quota') {
      this.setData({current_index_quota:i,total:this.data.total_array[i]})
    }
    this.get_activity();
  },
  get_activity() {
    let json = {
      time: this.data.time_array[this.data.current_index_time],
      quota: this.data.quota_array[this.data.current_index_quota],
      total: this.data.total_array[this.data.current_index_quota]
    }
    app._getNetWork({
      url:app.getNetAddresss("plugin.wise-yuan-trade.frontend.quality-activity.activity"),
      data:json,
      success:resdata=>{
        let {result,data,msg} = resdata.data;
        if(result){
          this.setData({
            id : data.id,
            rate : data.rate,
            usable_quota : data.usable_quota,
          })
        }else{
          app.tips(msg);
        }
      }
    })

  },
  confirmCategory() {
    app._postNetWork({
      url:app.getNetAddresss("plugin.wise-yuan-trade.frontend.quality-activity.join"),
      data:{id:this.data.id},
      success:resdata=>{
        let {result,msg}=resdata.data;
        app.tips(msg);
        if(result){
          this.setData({show:false})
          this.record();
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.page>=this.data.total_page){
      return;
    }
    let page = this.data.page + 1;
    this.record(page);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})